package com.xinqi.modules.course.textbook.convert;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.xinqi.common.mapstruct.CommonConvertConfig;
import com.xinqi.modules.course.textbook.domain.CourseCloudTextbookContentEntity;
import com.xinqi.modules.course.textbook.dto.req.CourseCloudTextbookContentCreateDTO;
import com.xinqi.modules.course.textbook.dto.req.CourseCloudTextbookContentUpdateDTO;
import com.xinqi.modules.course.textbook.dto.rsp.CourseCloudTextbookContentResultDTO;
import com.xinqi.modules.sys.textbook.dto.rsp.CloudTextbookContentResultDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

/**
 * 课程模块云课本课堂内容转换
 *
 * @author: wenjie
 * @date: 2023/05/30
 */
@Mapper(config = CommonConvertConfig.class)
public abstract class CourseCloudTextbookContentConvert {

    @Lazy
    @Autowired
    private IdentifierGenerator identifierGenerator;

    public abstract CourseCloudTextbookContentEntity create(CourseCloudTextbookContentCreateDTO s);

    public abstract CourseCloudTextbookContentEntity update(CourseCloudTextbookContentUpdateDTO s, @MappingTarget CourseCloudTextbookContentEntity t);

    public abstract CourseCloudTextbookContentEntity update(CourseCloudTextbookContentUpdateDTO s);

    @Mapping(target = "file", expression = "java(com.xinqi.common.base.utils.json.JSON.parse(s.getFile(), com.xinqi.common.base.dto.CommonFileDTO.class))")
    public abstract CourseCloudTextbookContentResultDTO convert(CourseCloudTextbookContentEntity s);

    //TODO
    @Mapping(target = "file", expression = "java(com.xinqi.common.base.utils.json.JSON.stringify(s.getFile()))")
    public abstract CourseCloudTextbookContentEntity convert(CloudTextbookContentResultDTO s);

    public CourseCloudTextbookContentEntity changeContent(CloudTextbookContentResultDTO s, Long refId) {
        CourseCloudTextbookContentEntity contentEntity = convert(s);
        contentEntity.setRefId(refId);
        contentEntity.setId(identifierGenerator.nextId(null).longValue());
        return contentEntity;
    }
}
